<?php

namespace App\Remote\MoreDo;

use Closure;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpFoundation\Request as RequestAlias;
use Symfony\Component\HttpFoundation\Response as ResponseAlias;

class AuthRemote extends Remote
{


    /**
     * @return Closure
     */
    public static function registerResponseHandle(): Closure
    {
        return function ($responseHttpCode, $responseOriginalContents) {
            if (
                $responseHttpCode == ResponseAlias::HTTP_OK
                &&
                $parsedResponse = jsonToArr($responseOriginalContents, [])
            ) {
                if (attributeGet($parsedResponse, "ret", null) == 200) {
                    return attributeGet($parsedResponse, "data", "");
                }
            }
            throw new \Exception($responseOriginalContents);
        }; // TODO: Change the autogenerated stub
    }

    /**
     * @return mixed
     */
    public static function getToken()
    {
        return Cache::store("redis")->remember("moreDo:authToken", 1.5 * 60 * 60, function () {
            return self::customRequestV3(
                "/web/api/request/getToken",
                RequestAlias::METHOD_POST,
                [
                    "accessKey" => config("moreDo.accessKey"),
                    "secretKey" => config("moreDo.secretKey")
                ]
            );
        });
    }
}
